﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BDS3M.Controls
{
    public partial class ChangeProfile : System.Web.UI.UserControl
    {
        public string Username;
        public string email;
        public string fullname;
        public string birthday;
        public string company;
        public string sex;
        public string Address;
        public int user_id;
        public string emailemployee;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Username"] == null)
            {
                Response.Redirect("/home");
            }
            if (!IsPostBack)
            {
                string username = Session["Username"].ToString();
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectAllTableWhereName("tbl_Employee", "Employee_User", username);
                txtUsername.Text = dt.Rows[0]["Employee_User"].ToString();
                txtEmail1.Text = dt.Rows[0]["Employee_Email"].ToString();
                txtName.Text = dt.Rows[0]["Employee_FullName"].ToString();


                ListItem lt = new ListItem((Convert.ToDateTime(dt.Rows[0]["Employee_BirthDay"])).Day.ToString(), (Convert.ToDateTime(dt.Rows[0]["Employee_BirthDay"])).Day.ToString());
                ddlDay.Items.Add(lt);
                lt = new ListItem((Convert.ToDateTime(dt.Rows[0]["Employee_BirthDay"])).Month.ToString(), (Convert.ToDateTime(dt.Rows[0]["Employee_BirthDay"])).Month.ToString());
                ddlMonth.Items.Add(lt);
                lt = new ListItem((Convert.ToDateTime(dt.Rows[0]["Employee_BirthDay"])).Year.ToString(), (Convert.ToDateTime(dt.Rows[0]["Employee_BirthDay"])).Year.ToString());
                ddlYear.Items.Add(lt);

                txtCompany.Text = dt.Rows[0]["Employee_Company"].ToString();
                ddlSex.Text = dt.Rows[0]["Employee_Sex"].ToString();
                txtAddress.Text = dt.Rows[0]["Employee_Address"].ToString();
                txtDienThoai.Text = dt.Rows[0]["Employee_HomePhone"].ToString();
                txtDiDong.Text = dt.Rows[0]["Employee_PhoneNumber"].ToString();

            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string username = Session["Username"].ToString();
            if (txtEmail1.Text != "")
            {
                if (txtName.Text != "")
                {
                    if (txtCompany.Text != "")
                    {
                        if (ddlSex.Text != "")
                        {
                            if (txtAddress.Text != "")
                            {
                                if (txtDienThoai.Text != "")
                                {
                                    if (txtDiDong.Text != "")
                                    {
                                        DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[7];
                                        cl[0] = new DAL.DHelpers.Column("Employee_Email", txtEmail1.Text, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                                        cl[1] = new DAL.DHelpers.Column("Employee_FullName", txtName.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                                        cl[2] = new DAL.DHelpers.Column("Employee_Company", txtCompany.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                                        cl[3] = new DAL.DHelpers.Column("Employee_Sex", ddlSex.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                                        cl[4] = new DAL.DHelpers.Column("Employee_Address", txtAddress.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                                        cl[5] = new DAL.DHelpers.Column("Employee_HomePhone", txtDienThoai.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);
                                        cl[6] = new DAL.DHelpers.Column("Employee_PhoneNumber", txtDiDong.Text, DAL.DHelpers.DataType.DataTypeSql.Nvarchar);

                                        string[] colWhere = new string[1];
                                        colWhere[0] = "Employee_User";

                                        object[] colValue = new object[1];
                                        colValue[0] = username;

                                        DAL.DCollector.DBCollector.Instance.Update("tbl_Employee", cl, colWhere, colValue);

                                        Response.Redirect("/thaydoithongtin"); 
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}